package compnents

import (
	"centerServer/models"
	"encoding/json"
	"github.com/astaxie/beego/logs"
	"io/ioutil"
)

func WXGetUserInfo(accessToken string, openid string) (*models.RequestWXUserInfo, error) {

	/*
	https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
	{
	  "openid": "OPENID",
	  "nickname": "NICKNAME",
	  "sex": 1,
	  "province": "PROVINCE",
	  "city": "CITY",
	  "country": "COUNTRY",
	  "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
	  "privilege": ["PRIVILEGE1", "PRIVILEGE2"],
	  "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
	}
	*/

	var requestBack models.RequestWXUserInfo
	requestUrl := WXUSERINFO_URL + "access_token=" + accessToken + "&openid=" + openid
	httpReques, errA := HttpGet(requestUrl)
	if errA != nil {
		logs.Error("WXGetUserInfo err :" + errA.Error())
		return &requestBack, errA
	}
	defer httpReques.Body.Close()
	bodyByte, _ := ioutil.ReadAll(httpReques.Body)
	jErr := json.Unmarshal(bodyByte, &requestBack)
	if jErr != nil {
		logs.Error("WXGetUserInfo err :" + jErr.Error())
		return &requestBack, jErr
	}
	return &requestBack, nil
}